﻿using UnityEngine;
using System.Collections;
using System;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{

    [RequireComponent(typeof(UIEventListener))]
    public class FestivalTabs : MonoBehaviour
    {
        [HideInInspector]
        public ActivityType type;//这个是tab的type
        [HideInInspector]
        public string activityId;
        public GameObject redPoint;
        public GameObject chosed;
        public GameObject normal;
        public UILabel activityTime;
        public SimpleGrid simpleGrid;
        public UIScrollView scrollView;
        public UILabel normalLabel;
        public UILabel chosedLabel;
        public UILabel topTitle;
        GameObject itemPref;
        UIActivityFestival script;
        /// <summary>
        /// 协议格式 CSActivityInfo CSActivityShopInfo CSCopyBoonInfo
        /// </summary>
        int msgType;
        void Start() {
            GetComponent<UIEventListener>().onClick = OnBtnClick;
        }

        public void OnBtnClick(GameObject button)
        {
            SetChosedState();
            msgType = 0;//作为一个默认状态
            switch (type)
            {
                case ActivityType.BattleRace:
                    itemPref = script.itemPrefs[4];
                    script.SetBgAndPerson(8);
                    break;
                case ActivityType.CostTotal:
                case ActivityType.RechargeTotal:
                case ActivityType.RechargeSurprise:
                    msgType = 4;
                    script.SetBgAndPerson(4);
                    itemPref = script.itemPrefs[1];
                    break;
                case ActivityType.LoginAward:
                    script.SetBgAndPerson(4);
                    msgType = 4;
                    itemPref = script.itemPrefs[1];
                    break;
                case ActivityType.MainCopy:
                case ActivityType.RobTrea:
                    itemPref = script.itemPrefs[4];
                    script.SetBgAndPerson(9);
                    break;
                case ActivityType.PrizeTask:
                    itemPref = script.itemPrefs[4];
                    script.SetBgAndPerson(10);
                    break;
                case ActivityType.PrizeBoss:
                    itemPref = script.itemPrefs[4];
                    script.SetBgAndPerson(11);
                    break;
                case ActivityType.CopyBenefits:
                    msgType = 2;
                    break;
                case ActivityType.DiscountSale:
                    msgType = 1;
                    script.SetBgAndPerson(1);
                    break;
                case ActivityType.HalfDiscountSale:
                    msgType = 1;
                    script.SetBgAndPerson(0);
                    break;
                case ActivityType.AincradStars:
                    itemPref = script.itemPrefs[4];
                    script.SetBgAndPerson(10);
                    break;
                case ActivityType.Congratulation:
                    msgType = 4;
                    itemPref = script.itemPrefs[1];
                    script.SetBgAndPerson(6);
                    break;
                case ActivityType.OpenHappy:
                    msgType = 4;
                    itemPref = script.itemPrefs[1];
                    script.SetBgAndPerson(5);
                    break;
                case ActivityType.TimeLimitExchange:
                    msgType = 1;
                    script.SetBgAndPerson(2);
                    break;
                case ActivityType.TimeLimitSail:
                    msgType = 1;
                    script.SetBgAndPerson(3);                   
                    break;
                default:
                    Debug.LogError("type可能没赋值成功==" + type);
                    return;
            }
            for (int i = 0; i < script.itemPrefs.Length; i++)
                script.itemPrefs[i].SetActive(false);

            GetComponentInParent<UIActivity>().options["tabType"] = type.ToString();
            script.normalContent.SetActive(true);
            script.copyContent.SetActive(false);
            if (msgType == 0 || msgType == 4)
            {
                CSActivityInfo msg = new CSActivityInfo();
                msg.activityId = this.activityId;
                NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, ActivityInfoCallback);
            }
            else if(msgType == 1)
            {
                CSActivityShopInfo msg = new CSActivityShopInfo();
                msg.activityId = this.activityId;
                NetAPI.SendHttp(OpDefine.CSActivityShopInfo, msg, ActivityShopInfoCallback);
            }
            else if (msgType == 2)
            {
                script.normalContent.SetActive(false);
                script.copyContent.SetActive(true);
                var copy = script.copyContent.GetComponent<CopyBenefit>();
                copy.activityId = this.activityId;
                copy.Show();
            }
            

        }

        void ActivityInfoCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;
                if (msg.itemList.Count <= 0)
                {
                    Debug.Log("活动列表为0");
                    return;
                }
                activityId = msg.activityId;

                //活动时间和领取截止时间
                DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                string acTime = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;
                if (msg.param == "1")
                    activityTime.text = string.Format(TableTextBlo.GetText("activity_time_Format"), acTime);
                else
                {
                    DateTime deadTime = ServerTimerTool.Java2CSTimeLocale(msg.rewardEndTime);
                    acTime = deadTime.Month.ToString() + TableTextBlo.GetText("month") + deadTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", deadTime.Hour, deadTime.Minute, deadTime.Second);
                    activityTime.text = string.Format(TableTextBlo.GetText("getReward_DeadTime"), acTime);
                }
                topTitle.text = msg.information;

                if (msgType == 4)
                {
                    UpdateReChargeList(msg.itemList);
                }
                else if(msgType == 0)
                {
                    //if (itemPref == script.itemPrefs[1])
                    //    UpdateLoginList(msg.itemList);
                    //else 
                    if (itemPref == script.itemPrefs[4] || itemPref == script.itemPrefs[5])
                        UpdateVsList(msg.itemList);
                }
            }
        }

        void ActivityShopInfoCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityShopInfo msg = packet.kBody as SCActivityShopInfo;

                if (msg.shopItemList.Count <= 0)
                {
                    Debug.Log("活动列表为0");
                    return;
                }
                activityId = msg.activityId;
                //时间
                System.DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                System.DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                activityTime.text = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;
                topTitle.text = msg.param;
                UpdateSailItemList(msg.shopItemList);
            }
        }

        void UpdateVsList(List<ActivityItem> list)
        {
            foreach (var item in simpleGrid.childs)
                Destroy(item);
            simpleGrid.childs.Clear();
            ResetCanGetNum();
            for (int i = 0; i < list.Count; i++)
            {
                ActivityItem item = list[i];
                if (item.rewardList.Count > 1)
                {
                    itemPref = script.itemPrefs[5];
                    GameObject obj = Instantiate(itemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    var vsGetItem = obj.GetComponent<VsGetItem>();
                    vsGetItem.type = type;
                    vsGetItem.tabType = script.activityType;
                    vsGetItem.m_update = UpdateVsList;
                    vsGetItem.m_addNum = AddCanGetNum;
                    vsGetItem.SetActivityMultiItem(item, activityId);
                }
                else
                {
                    itemPref = script.itemPrefs[4];
                    GameObject obj = Instantiate(itemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    var vsGetItem = obj.GetComponent<VsGetItem>();
                    vsGetItem.type = type;
                    vsGetItem.tabType = script.activityType;
                    vsGetItem.m_update = UpdateVsList;
                    vsGetItem.m_addNum = AddCanGetNum;
                    vsGetItem.SetActivityItem(item, activityId);
                }
            }
            SetRedByNum();
            scrollView.ResetPosition();
            itemPref.SetActive(false);
            if (list.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;
        }

        public void UpdateSailItemList(List<ActivityShopItem> list)
        {
            if (list != null)
            {
                list.Sort(delegate (ActivityShopItem a, ActivityShopItem b)
                {
                    if (a.index < b.index)
                        return -1;
                    else if (a.index > b.index)
                        return 1;
                    return 0;
                });
            }

            foreach (var item in simpleGrid.childs)
                Destroy(item);
            simpleGrid.childs.Clear();

            for (int i = 0; i < list.Count; i++)
            {
                if (type == ActivityType.TimeLimitExchange || type == ActivityType.TimeLimitSail)
                {
                    itemPref = script.itemPrefs[2];
                    GameObject obj = Instantiate(itemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    var onSailItem = obj.GetComponent<OnSailItem>();
                    onSailItem.m_update = UpdateSailItemList;
                    onSailItem.SetItem(list[i], activityId);
                }
                else
                {
                    if (list[i].getResource.Count > 1)
                    {
                        itemPref = script.itemPrefs[0];
                        GameObject obj = Instantiate(itemPref);
                        obj.SetActive(true);
                        simpleGrid.AddChild(obj);
                        var onSailItem = obj.GetComponent<OnSailItem>();
                        onSailItem.m_update = UpdateSailItemList;
                        onSailItem.SetReward(list[i], activityId);
                    }
                    else {
                        itemPref = script.itemPrefs[3];
                        GameObject obj = Instantiate(itemPref);
                        obj.SetActive(true);
                        simpleGrid.AddChild(obj);
                        var onSailItem = obj.GetComponent<OnSailItem>();
                        onSailItem.m_update = UpdateSailItemList;
                        onSailItem.SetSingleReward(list[i], activityId);
                    }
                }
            }
            itemPref.SetActive(false);
            if (list.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;

            //scrollView.ResetPosition();
        }


        void UpdateReChargeList(List<ActivityItem> list)
        {
            foreach (var item in simpleGrid.childs)
                Destroy(item);
            simpleGrid.childs.Clear();
            ResetCanGetNum();
            itemPref.SetActive(true);
            for (int i = 0; i < list.Count; i++)
            {
                GameObject obj = Instantiate(itemPref);
                simpleGrid.AddChild(obj);
                var script = obj.GetComponent<AcRechargeSingleItem>();
                script.m_callback = UpdateReChargeList;
                script.type = type;
                script.m_addNum = AddCanGetNum;
                script.SetActivityItem(list[i], activityId);
            }
            SetRedByNum();
            itemPref.SetActive(false);
            scrollView.ResetPosition();
            if (list.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;
        }
        void SetChosedState()//设置tab页的选中状态
        {
            script = GetComponentInParent<UIActivityFestival>();

            if (normal == script.normalObj)
                return;
            normal.SetActive(false);
            chosed.SetActive(true);
            script.normalObj.SetActive(true);
            script.chosedObj.SetActive(false);

            script.normalObj = normal;
            script.chosedObj = chosed;
        }



        #region 红点相关
        int[] canGet = new int[15];
        public void ResetCanGetNum()
        {
            for (int i = 0; i < canGet.Length; i++)
                canGet[i] = 0;
        }

        public void AddCanGetNum()
        {
            switch (type)
            {
                case ActivityType.LoginAward://累计登陆
                    canGet[0]++;
                    break;
                case ActivityType.OpenHappy://开服狂欢
                    canGet[1]++;
                    break;
                case ActivityType.Congratulation://庆典活动
                    canGet[2]++;
                    break;
                case ActivityType.RechargeSurprise://单充送礼
                    canGet[3]++;
                    break;
                case ActivityType.BattleRace://竞技场
                    canGet[4]++;
                    break;
                case ActivityType.RobTrea://夺宝
                    canGet[5]++;
                    break;
                case ActivityType.PrizeTask://悬赏任务
                    canGet[6]++;
                    break;
                case ActivityType.PrizeBoss://悬赏boss
                    canGet[7]++;
                    break;
                case ActivityType.MainCopy://主线副本
                    canGet[8]++;
                    break;
                case ActivityType.CostTotal://累计消费
                    canGet[9]++;
                    break;
                case ActivityType.RechargeTotal://累充奖励
                    canGet[10]++;
                    break;
                case ActivityType.AincradStars://试炼之地
                    canGet[11]++;
                    break;
            }
        }

        void RedPointUpdate0(RedPointTip.Red red)
        {
            script.ReduceRedPoint();
            redPoint.SetActive(false);
            StaticData.redTips.SetPointState(red, 0);
        }

        public void SetRedByNum()
        {
            if (!redPoint.activeInHierarchy)
            {
                return;
            }
            switch (type)
            {
                case ActivityType.LoginAward:
                    if (canGet[0] < 1)
                    {
                        if (redPoint.activeInHierarchy)
                        {
                            script.ReduceRedPoint();
                            redPoint.SetActive(false);
                            if (script.activityType == ActivityType.Festival)
                                StaticData.redTips.SetPointState(RedPointTip.Red.totalLoginFestival, 0);
                            else if (script.activityType == ActivityType.Update)
                                StaticData.redTips.SetPointState(RedPointTip.Red.totalLoginUpdate, 0);
                        }
                    }
                    break;
                case ActivityType.OpenHappy:
                    if (canGet[1] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.openServerSpreeFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.openServerSpreeUpdate, 0);
                    }
                    break;
                case ActivityType.Congratulation:
                    if (canGet[2] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.celebrationActivityFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.celebrationActivityUpdate, 0);
                    }
                    break;
                case ActivityType.RechargeSurprise:
                    if (canGet[3] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.rechargeSingleFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.rechargeSingleUpdate, 0);
                    }
                    break;
                case ActivityType.BattleRace:
                    if (canGet[4] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.pvpWinTimesFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.pvpWinTimesUpdate, 0);
                        StaticData.redTips.SetPointState(RedPointTip.Red.pvpWinTimesFestival, 0);
                    }
                    break;
                case ActivityType.RobTrea:
                    if (canGet[5] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.robTreasureTimesFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.robTreasureTimesUpdate, 0);
                    }
                    break;
                case ActivityType.PrizeTask:
                    if (canGet[6] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.fightRedNameTimesFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.fightRedNameTimesUpdate, 0);
                    }
                    break;
                case ActivityType.PrizeBoss:
                    if (canGet[7] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.killRedNameTimesFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.killRedNameTimesUpdate, 0);
                    }
                    break;
                case ActivityType.MainCopy:
                    if (canGet[8] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.mainCopyWinTimesFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.mainCopyWinTimesUpdate, 0);
                    }
                    break;
                case ActivityType.CostTotal:
                    if (canGet[9] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.costTotalFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.costTotalUpdate, 0);
                    }
                    break;
                case ActivityType.RechargeTotal:
                    if (canGet[10] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.rechargeTotalFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.rechargeTotalUpdate, 0);
                    }
                    break;
                case ActivityType.AincradStars:
                    if (canGet[11] < 1)
                    {
                        script.ReduceRedPoint();
                        redPoint.SetActive(false);
                        if (script.activityType == ActivityType.Festival)
                            StaticData.redTips.SetPointState(RedPointTip.Red.aincradStarsFestival, 0);
                        else if (script.activityType == ActivityType.Update)
                            StaticData.redTips.SetPointState(RedPointTip.Red.aincradStarsUpdate, 0);
                    }
                    break;
            }
        }

        #endregion
    }
}

